﻿Friend Class FHandleMissingFeatureActivity
    Private mActivityEx As MissingFeatureActivity
    Private mItem As Entities.SharedItem

    Public Shadows Sub Show(ByVal activity As MissingFeatureActivity)
        mItem = activity.Item
        mActivityEx = activity
        InitView(activity.Item)
        MyBase.Show()
    End Sub
    Public Shadows Sub Show(ByVal item As Entities.SharedItem)
        mItem = item
        InitView(item)
        MyBase.Show()
    End Sub
    Private Sub InitView(ByVal item As Entities.SharedItem)
        Me.Icon = My.Resources.Movie
        Me.Text = String.Format("Supply feature information for movie {0} ({1})", item.Name, item.Comment)
        txtSharedItemTitle.Text = item.Name
        txtSharedItemComment.Text = item.Comment
        txtTitle.Text = item.Name
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If mActivityEx IsNot Nothing Then
            mActivityEx.Close()
            Framework.Activities.ToDoProvider.OnActivityUpdated(mActivityEx)
        End If
        Me.Close()
    End Sub

    Private mRunningSearch As RunningSearch

    Private Class RunningSearch
        Public GetImdbIdByTitleResult As OpenMediaInfo.IMDB.Search.GetImdbIdByTitleResult
        Public GetAllTitlesFromSearchResult As Generic.List(Of OpenMediaInfo.IMDB.Search.FoundTitle)
        Public Exception As Exception
    End Class

    Private Sub EventHandler_Search()
        Me.UseWaitCursor = True
        Try
            rbSearchIMDB.Checked = True
            btnSearch.Enabled = False
            lvwFoundTitles.Items.Clear()
            lvwFoundTitles.Items.Add("Searching...")

            mRunningSearch = New RunningSearch
            Threading.ThreadPool.QueueUserWorkItem(AddressOf EventHandler_SearchWorker, mRunningSearch)
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        Finally
            btnSearch.Enabled = True
            Me.UseWaitCursor = False
        End Try
    End Sub
    Private Sub EventHandler_SearchWorker(ByVal state As Object)
        Try
            Dim clsSearch As RunningSearch = CType(state, RunningSearch)
            Try
                Dim strTitle As String = OpenMediaInfo.IMDB.Search.CleanTitleForSearch(txtTitle.Text)
                Dim strYear As String = Engine.Manager.FindYearInComment(txtSharedItemComment.Text)
                clsSearch.GetImdbIdByTitleResult = OpenMediaInfo.IMDB.Search.GetImdbIdByTitle(strTitle)
                If clsSearch.GetImdbIdByTitleResult.Id Is Nothing Then
                    Dim uriSearch As Uri = OpenMediaInfo.IMDB.Search.GetSearchTitleUri(strTitle)
                    Dim aItems As Generic.List(Of OpenMediaInfo.IMDB.Search.FoundTitle)
                    aItems = OpenMediaInfo.IMDB.Search.GetAllTitlesFromSearchResult(uriSearch)

                    clsSearch.GetAllTitlesFromSearchResult = aItems
                End If
            Catch ex As Exception
                clsSearch.Exception = ex
            End Try
            Framework.Threading.Invoke(Of RunningSearch)(AddressOf EventHandler_SearchFinished, clsSearch)
        Catch ex As Exception
            Framework.Misc.HandleException(ex)
        End Try
    End Sub
    Private Sub EventHandler_SearchFinished(ByVal search As RunningSearch)
        Try
            If search IsNot mRunningSearch Then Return
            If search.Exception IsNot Nothing Then
                Framework.Misc.HandleException(Me, search.Exception)
                Return
            End If

            Dim clsBestMatch As ListViewItem = Nothing
            Dim clsMatch As ListViewItem = Nothing

            lvwFoundTitles.BeginUpdate()
            Try
                lvwFoundTitles.Items.Clear()

                Dim strTitle As String = OpenMediaInfo.IMDB.Search.CleanTitleForSearch(txtTitle.Text)
                Dim clsRes As OpenMediaInfo.IMDB.Search.GetImdbIdByTitleResult = search.GetImdbIdByTitleResult
                If clsRes.Id IsNot Nothing Then
                    Dim clsItem As ListViewItem = lvwFoundTitles.Items.Add(clsRes.Id)
                    With clsItem
                        .SubItems.Add("Single match")
                        .SubItems.Add(strTitle)
                        .SubItems.Add(String.Empty)
                        clsItem.Tag = OpenMediaInfo.IMDB.Manager.GetUriFromId(clsRes.Id)
                    End With
                    clsMatch = clsItem
                    clsBestMatch = clsItem
                    lvwFoundTitles.Focus()
                Else
                    Dim strYear As String = Engine.Manager.FindYearInComment(txtSharedItemComment.Text)

                    For Each clsTitle As OpenMediaInfo.IMDB.Search.FoundTitle In search.GetAllTitlesFromSearchResult
                        Dim clsItem As ListViewItem = lvwFoundTitles.Items.Add(clsTitle.Id)
                        With clsItem
                            .SubItems.Add(clsTitle.Type.ToString)
                            .SubItems.Add(clsTitle.Name)
                            .SubItems.Add(clsTitle.Year)
                            If clsTitle.Name.ToLower.Trim = strTitle.ToLower.Trim Then
                                clsMatch = clsItem
                                If strYear IsNot Nothing AndAlso clsTitle.Year.Trim = strYear.Trim Then clsBestMatch = clsItem
                            End If
                            .Tag = clsTitle.Uri
                        End With
                    Next
                    If lvwFoundTitles.Items.Count > 0 Then
                        lvwFoundTitles.Focus()
                    Else
                        txtTitle.Focus()
                    End If
                End If
            Finally
                lvwFoundTitles.EndUpdate()
            End Try

            If clsBestMatch Is Nothing Then clsBestMatch = clsMatch
            If clsBestMatch IsNot Nothing Then
                clsBestMatch.Selected = True
                lvwFoundTitles.EnsureVisible(clsBestMatch.Index)
                lvwFoundTitles.Focus()
            Else
                txtTitle.Focus()
            End If
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        EventHandler_Search()
    End Sub

    Private Sub txtTitle_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTitle.GotFocus
        Me.AcceptButton = btnSearch
    End Sub

    Private Sub txtTitle_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTitle.LostFocus
        Me.AcceptButton = btnOK
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        EventHandler_Ok()
    End Sub
    Private Sub EventHandler_Ok()
        Try
            btnOK.Enabled = False
            Me.UseWaitCursor = True
            Application.DoEvents()

            If rbSearchIMDB.Checked Then
                If lvwFoundTitles.SelectedItems.Count = 0 Then
                    Beep() : Return
                End If
                Dim uriTitle As Uri = CType(lvwFoundTitles.SelectedItems(0).Tag, Uri)
                Engine.Manager.UpdateMediaInfoForExistingItem(mItem, uriTitle, Engine.CreateMediaInfoMode.ForceHintImdbUriAsFeature)
                Me.Close()
            ElseIf rbEnterOwnIMDBUri.Checked Then
                If OpenMediaInfo.IMDB.Manager.IsImdbTitlePage(txtIMDBTitleUrl.Text) Then
                    Dim uriTitle As New Uri(txtIMDBTitleUrl.Text)
                    Engine.Manager.UpdateMediaInfoForExistingItem(mItem, uriTitle, Engine.CreateMediaInfoMode.ForceHintImdbUriAsFeature)
                    Me.Close()
                End If
            ElseIf rbNoFeatureAvailable.Checked Then
                Engine.Manager.UpdateMediaInfoForExistingItem(mItem, Engine.CreateMediaInfoMode.TagAsFeatureNotAvailable)
                Me.Close()
            Else
                Beep()
            End If
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        Finally
            btnOK.Enabled = True
            Me.UseWaitCursor = False
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnSearch_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        EventHandler_Search()
    End Sub

    Private Sub tmrStartSearch_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrStartSearch.Tick
        tmrStartSearch.Enabled = False
        EventHandler_Search()
    End Sub

    Private Sub txtIMDBTitleUrl_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtIMDBTitleUrl.TextChanged
        rbEnterOwnIMDBUri.Checked = True
    End Sub

    Private Sub txtTitle_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTitle.TextChanged
        rbSearchIMDB.Checked = True
    End Sub

    Private Sub lvwFoundTitles_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwFoundTitles.DoubleClick
        EventHandler_Ok()
    End Sub

    Private Sub FHandleMissingFeatureActivity_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        mRunningSearch = Nothing
    End Sub
End Class